<?php
/* explain:
 * Project:     
 * File:        Job.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
class Operation extends Common
{
	public $conHandle;
	public $errorInfo;
	public static $SelfFileName;

	function Operation($strLanguage = "en-US")
	{
		parent::Common();
		$this->conHandle = $this->getConnection("system");
		$this->strLanguage = $strLanguage;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getFromID($arrPopedom = array(), $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM operation WHERE id = $id";
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* get data via groupid and menuid
	 *
	 * @param  int    id
	 * @return operate array
	 */
	
	public function arrGetFromGroupID($groupid, $menuid)
	{
		if($groupid == 1){
			$sql = "SELECT id FROM operation";
			$rs = mysql_query($sql, $this->conHandle);
			
			if($rs){
				$arrCache = array();
				while ($arrData = mysql_fetch_array($rs)){
					$arrCache[] = $arrData['id'];
				}
				return $arrCache;
			}
		}else{
			$sql = "SELECT operate FROM group_authority WHERE menuid = '$menuid' AND groupid = '$groupid'";
			$rs = mysql_query($sql, $this->conHandle);
			
			if($rs){
				if($arrData = mysql_fetch_array($rs)){
					return explode(',', $arrData['operate']);
				}
			}
		}
		
		return false;
	}
	
/* get data via groupid and menuid
	 *
	 * @param  int    id
	 * @return operate array
	 */
	
	public function arrGetFromGroupIDAndMenuID($groupid, $menuid)
	{
		$sql = "SELECT operate FROM group_authority WHERE menuid = '$menuid' AND groupid = '$groupid'";
		$rs = mysql_query($sql, $this->conHandle);
		
		if($rs){
			if($arrData = mysql_fetch_array($rs)){
				return explode(',', $arrData['operate']);
			}
		}
		
		return array();
	}
	
	/* 
	 *
	 * @param  string  	name 	
	 * @param  string 	namecn 
	 * @return boolean 	true or fail
	 * 
	 */
	public function add($arrPopedom = array(), $name)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "INSERT INTO operation SET name = '$name'";
		$rs = mysql_query($sql, $this->conHandle) or die(mysql_error());  
		return $rs ? true: false;
	}
		
	/* modify menu
	 *
	 * @param  string 	id			modify menu id
	 * @param  string 	strOrders	
	 * @param  string  	name 	
	 * @param  string 	namecn 
	 * @return boolean 	true or fail
	 */
	public function edit($arrPopedom = array(), $id, $name)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "UPDATE operation SET name = '$name', WHERE id='$id'";
		$rs = mysql_query($sql, $this->conHandle) or die ('update error : ' . mysql_error($this->conHandle).$sql);  
		return $rs ? true: false;
	}
			
	/* delete menu
	 *
	 * @param  int    	id
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "DELETE FROM operation WHERE id = $id";
		$rs	 = mysql_query($sql, $this->conHandle);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* list menu
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $condition, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());

		$arrback = array();

		$sql = "SELECT count(id) FROM operation$condition";

		$rs  = mysql_query($sql, $this->conHandle);
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = "SELECT * FROM operation$condition";
		else{
			$sql = "SELECT * FROM operation$condition LIMIT $this->firstcount, $discount";
		}
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* list all custom condition data
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listFromCustom($arrPopedom = array(), $condition='')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());

		$strViewName = "name as viewName";
			
		$arrBack = array();
		$sql = "SELECT id, name, $strViewName FROM operation$condition";
        $rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}
	
	/* operation list
	 *
	 * @ param  return 	array		�� data array
	 */
	public function listOperation()
	{
		$sql = "SELECT id, name FROM operation";
			
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			$arrback = array();
			
			$arrID		= array();
			$arrName 	= array();
			while(is_array($arrcache = mysql_fetch_array($rs))){
				$arrID[] 	= $arrcache['id'];
				$arrName[] 	= $arrcache['name'];
			}
			$arrback['id'] 		= $arrID;
			$arrback['name'] 	= $arrName;
			
			return $arrback;
		}else
 			return false;
	}

	public function arrGetOPInfo($arrOperate)
	{
		$arrOperateInfo = array();
		
		if(count($arrOperate) > 0){
			$strQuery = $_SERVER["REQUEST_URI"];
			$arrUrlInfo 	= pathinfo($strQuery);
			Operation::$SelfFileName = $arrUrlInfo['filename'];
			
			$arrOperateAllInfo = $this->listFromCustom($arrOperate, " WHERE id in (".implode(",", $arrOperate).")");
			
			$i = 0; 
			foreach($arrOperateAllInfo as $key => $value){
				$strFileName = Operation::$SelfFileName."_".$value['name'].".php";
		
				if(file_exists($strFileName)){
					$arrOperateInfo[$i]['name'] 		= $value['name'];
					$arrOperateInfo[$i]['viewName'] 	= $value['name'];
					
					$i++;
				}
			}
		}
		
		return $arrOperateInfo;
	}
	
	public function strGetSelfFileName()
	{
		return Operation::$SelfFileName;
	}
}
?>
